import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import MaxNLocator, MultipleLocator
import sys
from os.path import abspath, dirname
import pickle

plt.rc('font', family='Arial', weight='normal')

if len(sys.argv) <= 1:
    print("using parm like dcqcn or hp95")
    exit(-1)
cc = sys.argv[1]

# 准备数据
with open(f"data/throughput_{cc}.txt") as f:
    datalines = f.readlines()

x_labels = ['30%', '60%', '90%']
x = np.arange(len(x_labels))  # x 轴的位置，使用 numpy 的 arange 生成连续的位置

y_origin = [0, 0, 0]
y_diffcs = [0, 0, 0]
for i in range(0, len(datalines), 2):
    y_origin[i // 2] = int(datalines[i].strip()) * 8 * 2 / 320 / 1e9
    y_diffcs[i // 2] = int(datalines[i + 1].strip()) * 8 * 2 / 320 / 1e9

# 设置通用绘图的样式
plt.figure(figsize=(12, 8))
plt.tick_params(labelsize=40)
font1 = {'weight': 'normal', 'size': 40}
X_name = "Workload"
Y_name = "Avg Throughput(Gbps)"

colors = ['#1f77b4', '#ff7f0e']  # 只使用两个颜色

width = 0.35  # 设置柱状图宽度

# 绘制柱状图
bars_origin = plt.bar(x - width / 2, y_origin, label='Origin', width=width, color=colors[0], edgecolor='black', alpha=0.8)  # Origin 的位置偏移
bars_diffcs = plt.bar(x + width / 2, y_diffcs, label='DiffCS', width=width, color=colors[1], edgecolor='black', alpha=0.8)  # DiffCS 的位置偏移

plt.xticks(x, x_labels, fontsize=40)  # 设置 x 轴标签的位置和字体大小

for bar in bars_origin:
    height = bar.get_height()
    plt.text(bar.get_x() + bar.get_width() / 2, height, f'{height:.4f}', ha='center', va='bottom', fontsize=20)  # 'bottom' 将文本放置在柱状图上方

for bar in bars_diffcs:
    height = bar.get_height()
    plt.text(bar.get_x() + bar.get_width() / 2, height, f'{height:.4f}', ha='center', va='bottom', fontsize=20)  # 'bottom' 将文本放置在柱状图上方
    
# 添加标签和标题
plt.xlabel(X_name, fontdict=font1)
plt.ylabel(Y_name, fontdict=font1)
plt.legend(fontsize=30)

# 保存图像
png_filename = f'img/th_{cc}.pdf'
plt.savefig(png_filename, format='pdf', dpi=300, bbox_inches='tight')
png_filename = f'img/th_{cc}.png'
plt.savefig(png_filename, format='png', dpi=300, bbox_inches='tight')

print(f"Finish draw th_{cc}.pdf")
plt.cla()
